using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Microsoft.PowerShell.CmdletManagement
{
    public partial class SpecExamplesControl : System.Windows.Controls.UserControl
    {
        public SpecExamplesControl()
        {
            InitializeComponent();
        }

        protected override void OnRender(DrawingContext drawingContext)
        {
            CmdletDesignerUtils.DisableAllCustomControls(this);

            if (CmdletDesignerUtils.IsInRole(PsSpecProjectRole.SpecWriter, Application.Current))
            {
                SpecExamples.IsReadOnly = false;
            }

            if (CmdletDesignerUtils.IsInRole(PsSpecProjectRole.HelpWriter, Application.Current))
            {
                HelpExamplesPanel.IsEnabled = true;
            }

            base.OnRender(drawingContext);
        }

        // The "new example" text box lost focus
        private void LostFocus_AddExample(object sender, System.Windows.RoutedEventArgs e)
        {
            AddCurentExampleToList();
        }

        // They pressed a key in the "Add Example" window
        private void KeyDown_AddExample(object sender, System.Windows.Input.KeyEventArgs e)
        {
            // If they pressed Enter, add it to the list of examples 
            if (e.Key == Key.Enter)
            {
                AddCurentExampleToList();
            }
        }

        // They clicked "Add an example before"
        private void Click_AddExampleBefore(object sender, RoutedEventArgs e)
        {
            PsSpecHelpExample senderItem = (PsSpecHelpExample) ((FrameworkElement)sender).DataContext;
            int itemIndex = HelpExamples.Items.IndexOf(senderItem);

            List<PsSpecHelpExample> listItems = new List<PsSpecHelpExample>();
            listItems.AddRange((PsSpecHelpExample[])HelpExamples.ItemsSource);

            PsSpecHelpExample newExample = new PsSpecHelpExample();

            listItems.Insert(itemIndex, newExample);
            HelpExamples.ItemsSource = listItems.ToArray();
        }

        // They clicked "Add an example after"
        private void Click_AddExampleAfter(object sender, RoutedEventArgs e)
        {
            PsSpecHelpExample senderItem = (PsSpecHelpExample)((FrameworkElement)sender).DataContext;
            int itemIndex = HelpExamples.Items.IndexOf(senderItem);

            List<PsSpecHelpExample> listItems = new List<PsSpecHelpExample>();
            listItems.AddRange((PsSpecHelpExample[])HelpExamples.ItemsSource);

            PsSpecHelpExample newExample = new PsSpecHelpExample();

            listItems.Insert(itemIndex + 1, newExample);
            HelpExamples.ItemsSource = listItems.ToArray();
        }

        // They clicked "Add an example before"
        private void Click_RemoveExample(object sender, RoutedEventArgs e)
        {
            MessageBoxResult result = MessageBox.Show(
                "Are you sure you wish to delete this help example?",
                "Delete this help example?",
                MessageBoxButton.YesNo);
            if (result != MessageBoxResult.Yes)
                return;

            PsSpecHelpExample senderItem = (PsSpecHelpExample)((FrameworkElement)sender).DataContext;
            int itemIndex = HelpExamples.Items.IndexOf(senderItem);

            List<PsSpecHelpExample> listItems = new List<PsSpecHelpExample>();
            listItems.AddRange((PsSpecHelpExample[])HelpExamples.ItemsSource);

            listItems.RemoveAt(itemIndex);
            HelpExamples.ItemsSource = listItems.ToArray();
        }

        // They pressed a key in the "Change Example" window
        private void KeyDown_ChangeExample(object sender, System.Windows.Input.KeyEventArgs e)
        {
            // On Ctrl-Enter, move the current help example down.
            // This lets you insert help examples.
            if ((e.Key == Key.Enter) && (Keyboard.Modifiers == ModifierKeys.Control))
            {
                ObservableCollection<PsSpecHelpExample> listItems =
                    (ObservableCollection<PsSpecHelpExample>)HelpExamples.ItemsSource;

                TextBox senderText = (TextBox)sender;

                for (int counter = 0; counter < listItems.Count; counter++)
                {
                    if (String.Equals(senderText.Text, listItems[counter].Command, 
                        StringComparison.CurrentCulture))
                    {
                        listItems.Insert(counter, new PsSpecHelpExample());
                        break;
                    }
                }
            }
        }

        // Add the current example (in the "New Example") textbox
        // to the list of examples
        private void AddCurentExampleToList()
        {
            if (!String.IsNullOrEmpty(NewExample.Text))
            {
                List<PsSpecHelpExample> listItems =
                    new List<PsSpecHelpExample>();
                listItems.AddRange((PsSpecHelpExample[]) HelpExamples.ItemsSource);

                PsSpecHelpExample newExample = new PsSpecHelpExample();
                newExample.Command = NewExample.Text;

                listItems.Add(newExample);
                NewExample.Text = "";

                HelpExamples.ItemsSource = listItems.ToArray();

                // Get the textbox that represents the output in the help example
                ContentPresenter uiElement = (ContentPresenter) HelpExamples.ItemContainerGenerator.ContainerFromItem(newExample);
                uiElement.Focus();
            }
        }
    }
}